﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static Butcher.winform.EnterpriseForms.MassSlaughter;
using System.Text.Json;
using Sunny.UI;
using System.Dynamic;
using static Butcher.winform.EnterpriseForms.ScaleBackfill;
using System.Diagnostics.Metrics;
using Traceability.Commont.Domains.AnimalsDomainFiles;
using Traceability.Commont.Domains.BatchManagementFiles;
using Traceability.Commont.DTOs.BatchManagementDTOs;
using Traceability.Commont.Domains;

namespace Butcher.winform.EnterpriseForms.Partitionees
{
    public partial class PartitionList : Form
    {
        string textbox41 = "";
        string ShoppingName = "";
        public PartitionList()
        {
            InitializeComponent();
            string url = @"http://localhost:5229/api/Section/PartitionSearch";
            HttpClient client = new HttpClient();
            Partition ctr = new Partition();
            var sh = 0;
            var content = new StringContent(JsonSerializer.Serialize<Partition>(ctr));
            content.Headers.Remove("Content-Type");
            content.Headers.Add("Content-Type", "application/json");
            string result = client.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;
            JObject info = JObject.Parse(result);
            var saaa = result;
            var aaa = info["date"].Value<JArray>();
            List<Partition> cts = aaa.ToObject<List<Partition>>();
            var c = 1;
            foreach (var ct in cts)
            {
                dataGridView1.DataSource = cts;
            }
            if (saaa != null)
            {
                sh = 1;
            }
            else
            {
                sh = 0;
            }
            tb_GrossWeight.LostFocus += new EventHandler(textBox2_GotFocus);
            getList();
        }
        private void uiFlowLayoutPanel2_Load(object sender, EventArgs e)
        {
            Shopping str = new Shopping();

            string url = @"http://localhost:5229/api/Section/ShopSearch";
            HttpClient client = new HttpClient();
            Clients ctr = new Clients();
            var sh = 0;
            var content = new StringContent(JsonSerializer.Serialize<Shopping>(str));
            content.Headers.Remove("Content-Type");
            content.Headers.Add("Content-Type", "application/json");
            string result = client.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;
            JObject info = JObject.Parse(result);
            var saaa = result;
            var aaa = info["date"].Value<JArray>();
            List<Shopping> cts = aaa.ToObject<List<Shopping>>();


            System.Windows.Forms.Button[] buttons = new System.Windows.Forms.Button[cts.Count];
            for (int i = 0; i < buttons.Length; i++)
            {
                buttons[i] = new System.Windows.Forms.Button();
                //buttons[i].Text = ssss[i].DuctionName; /*(i + 1).ToString();*/
                buttons[i].Location = new Point(i * 130 + 130, 30);
                Shopping asas = cts[i];
                //转换为字符串
                buttons[i].Name = Json.Serialize(asas);
                //转换为实体
                Shopping babab = Json.Deserialize<Shopping>(buttons[i].Name);
                buttons[i].Size = new Size(100, 80);
                buttons[i].BackColor = Color.White;
                buttons[i].ForeColor = Color.Black;
                //ScaleName.Text = ssss[i].ScaleName;
                buttons[i].Text = cts[i].ShoppingDesignationName;
                buttons[i].Click += new System.EventHandler(this.button_Click);
                uiFlowLayoutPanel2.Controls.Add(buttons[i]);
            }
        }
        private void button_Click(object sender, System.EventArgs e)
        {
            Button button = (Button)sender;
            ShoppingName = button.Text;
        }
        Partition tion = new Partition();
        private void button24_Click(object sender, EventArgs e)
        {

        }
        public void textBox2_GotFocus(object sender, EventArgs e)
        {
            ui_NetWeight.Text = (Convert.ToDecimal(tb_GrossWeight.Text) - Convert.ToDecimal(ui_tare.Text)).ToString();
            textbox41 = ui_NetWeight.Text;
        }
        Scale ScaleInfo = new Scale();
        public void getList()
        {
            var url = @"http://localhost:5229/api/Batch/WhiteBulkList";

            HttpClient client = new HttpClient();
            NEWPUT nEWPUT = new NEWPUT();
            var strValue = ScaleBackfill.TxtVal;
            if (!string.IsNullOrEmpty(strValue))
            {
                ScaleInfo = Json.Deserialize<Scale>(strValue);
                ui_tare.Text = ScaleInfo.TareWeightDeduction.ToString();
                getStash(ScaleInfo.Warehouse);
            }
            var content = new StringContent(JsonSerializer.Serialize<NEWPUT>(nEWPUT));
            content.Headers.Remove("Content-Type");
            content.Headers.Add("Content-Type", "application/json");
            string result = client.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;
            JObject info = JObject.Parse(result);
            var count = info["date"].Value<JArray>();
            List<BuclkWeight> enterprises = count.ToObject<List<BuclkWeight>>();

            foreach (var item in enterprises)
            {
                textBox8.Text = item.SlaughterNumber;
            }
        }
        private void uiSymbolButton2_Click(object sender, EventArgs e)
        {
            ScaleBackfill.pid = 3;
            ScaleBackfill scale = new ScaleBackfill();
            scale.Hide();
            scale.ShowDialog();
        }

        private void uiButton1_Click(object sender, EventArgs e)
        {
            tion.ShopId = 1;
            tion.ShoppingCodeId = ShoppingName;
            tion.RoughWeight = Convert.ToDecimal(tb_GrossWeight.Text);
            tion.PartitionTare = Convert.ToDecimal(ui_tare.Text);
            tion.NetWeight = Convert.ToDecimal(tb_GrossWeight.Text) - Convert.ToDecimal(ui_tare.Text);
            tion.PartitionNum = 1;
            tion.PartitionRemarks = textBox7.Text;
            tion.PartitionDate = DateTime.Now;
            tion.PartitionStart = Convert.ToInt32(textBox1.Text);
            HttpClient client = new HttpClient();
            var url = @"http://localhost:5229/api/Section/PartitionAdd";
            var content = new StringContent(JsonSerializer.Serialize<Partition>(tion));
            content.Headers.Remove("Content-Type");
            content.Headers.Add("Content-Type", "application/json");
            string result = client.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;
            JObject info = JObject.Parse(result);
            if (info != null)
            {
                MessageBox.Show("新增成功");
            }
            else
            {
                MessageBox.Show("新增失败");
            }
        }
        public void getStash(int id)
        {
            Stash stash = new Stash();
            string url = @"http://localhost:5229/api/Batch/StashList";
            HttpClient client = new HttpClient();
            var content = new StringContent(JsonSerializer.Serialize<Stash>(stash));
            content.Headers.Remove("Content-Type");
            content.Headers.Add("Content-Type", "application/json");
            string result = client.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;
            JObject info = JObject.Parse(result);
            var saaa = result;
            var aaa = info["date"].Value<JArray>();
            List<Stash> enterprises = aaa.ToObject<List<Stash>>();
            enterprises = enterprises.Where(x => x.Stashid == id).ToList();
            this.cb_Level.DataSource = enterprises;
            this.cb_Level.DisplayMember = "StashName";
            this.cb_Level.ValueMember = "Stashid";
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void uiSymbolButton1_Click(object sender, EventArgs e)
        {
            this.Hide();
            ScaleBackfill.pid = 3;
            ScaleBackfill scale = new ScaleBackfill();
            scale.Hide();
            scale.ShowDialog();
        }
    }
}
